package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageCategory;
import com.sky.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    /**修改分类*/
    public void updateCategory(CategoryDTO categoryDTO) {


        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        category.setUpdateTime(LocalDateTime.now());

        Long updateUser = BaseContext.getCurrentId();

        category.setUpdateUser(updateUser);

        categoryMapper.updateCategory(category);
    }

/**分类的分页查询*/

    @Override
    public PageCategory selectCategory(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());

        Page<Category> page = categoryMapper.selectCategory(categoryPageQueryDTO.getName(),categoryPageQueryDTO.getType());

        PageCategory pageCategory = new PageCategory();

        pageCategory.setTotal(page.getTotal());
        pageCategory.setRecords(page.getResult());
        return pageCategory;
    }
/**分类的状态开启/禁用*/
    @Override
    public void categoryStatus(Integer status, Long id) {
        Category category = new Category();

        category.setUpdateTime(LocalDateTime.now());

        Long updateUser = BaseContext.getCurrentId();

        category.setUpdateUser(updateUser);

        categoryMapper.categoryStatus(status,id,category.getUpdateUser(),category.getUpdateTime());
    }

    /**新增分类*/
    @Override
    public void insertCategory(CategoryDTO categoryDTO) {

        Category category = new Category();

        BeanUtils.copyProperties(categoryDTO,category);

        category.setUpdateTime(LocalDateTime.now());
        category.setCreateTime(LocalDateTime.now());

        Long updateUser = BaseContext.getCurrentId();

        category.setUpdateUser(updateUser);
        category.setCreateUser(updateUser);


        categoryMapper.insertCategory(category);
    }
/**删除分类*/
    @Override
    @Transactional
    public void deleteCategory(Integer id) {
        //查询dish表中是否有该分类关联的菜品
        List<Integer> list = categoryMapper.selectDishId(id);

        if(list.size() != 0 && list != null ){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }
        //查询当前分类是否关联了套餐
        List<Integer> list1 = categoryMapper.selectSetmealId(id);

        if(list1.size() != 0 && list1 != null ){
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }
        categoryMapper.deleteCategory(id);
    }
/**根据类型查询分类*/
    @Override
    public List<Category> selectByType(Integer type) {
        List<Category> list = categoryMapper.selectByType(type);
        return list;
    }

    @Override
    public List<Category> selectAll() {
        List<Category> categories = categoryMapper.selectAll();
        return categories;
    }
}
